//
//  SDTabBarController.m
//  skinDetection
//
//  Created by  liangzhun on 2018/10/29.
//  Copyright © 2018年  liangzhun. All rights reserved.
//

#import "SDTabBarController.h"
#import "SDHomeViewController.h"
#import "SDDetectionViewController.h"
#import "SDMineViewController.h"
#import "SDQrCodeViewController.h"

#import "SDTabbarItem.h"
#import "SDDetectionView.h"
#import "SDTabbarView.h"
#import "SDHomeShowView.h"

#import "SDDetectionDataModel.h"
#import "SDAnswerTestAllResultVC.h"

#import "SDAnswersDetectionModel.h"
@interface SDTabBarController ()

@property (nonatomic ,strong)SDTabbarView *tabbarView;

@property (nonatomic ,strong)SDTabbarItem *homeBtn;

@property (nonatomic ,strong)SDTabbarItem *mineBtn;

@property (nonatomic ,strong)SDTabbarItem *selectBtn;

@property (nonatomic ,strong)SDTabbarItem *detectionBtn;

@property (nonatomic ,strong)SDDetectionView *detectionView;

@property (nonatomic ,strong)SDQrCodeViewController *qrView;

@property (nonatomic ,strong) SDHomeShowView *kindView;

@end

@implementation SDTabBarController
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    //清空已有的检测数据
    SDDetectionDataModel *model = [SDDetectionDataModel shareInstance];
    [model clearData];
    [SDAnswersDetectionModel clearModel];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.tabBar.hidden = YES;
    self.viewControllers = @[[self setTabbarViewControl:[SDMethodTool getViewControlWithIdentifier:@"SDHomeViewController"]],[self setTabbarViewControl:[SDMethodTool getViewControlWithIdentifier:@"SDMineViewController"]]];
    [self layOutTabbarView];
    [self.detectionView starAnimation];
    self.tabbarView.goBtn = self.detectionView.goBtn;
    
}
- (void)layOutTabbarView
{
    [self.tabbarView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo(SDTabbar_Height);
        make.bottom.equalTo(self.view);
    }];
    self.selectBtn = self.homeBtn;
    [self.homeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.tabbarView);
        make.height.mas_equalTo(49);
        make.top.equalTo(self.tabbarView);
    }];
    
    [self.detectionBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.homeBtn.mas_right);
        make.width.equalTo(self.homeBtn);
        make.height.mas_equalTo(49);
        make.top.equalTo(self.tabbarView);
    }];

    [self.detectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.tabbarView);
        make.height.width.mas_equalTo(56);
        make.top.equalTo(self.tabbarView).offset(-24);
    }];
    
    [self.mineBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.detectionBtn.mas_right);
        make.right.equalTo(self.tabbarView);
        make.width.equalTo(self.homeBtn);
        make.height.mas_equalTo(49);
        make.top.equalTo(self.tabbarView);
    }];
    
    [self.kindView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.equalTo(self.view);
    }];
    
}
- (UINavigationController *)setTabbarViewControl:(UIViewController *)vc
{
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
    nav.navigationBarHidden = YES;
    return nav;

}
#pragma mark - 懒加载
- (SDTabbarView *)tabbarView
{
    if (!_tabbarView) {
        _tabbarView = [[SDTabbarView alloc] init];
        UIView *tabBg = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SDMainScreenWidth, SDTabbar_Height)];
        [tabBg setBackgroundColor:SDWhiteColor];
        [_tabbarView addSubview:tabBg];
        [_tabbarView setBackgroundColor:SDWhiteColor];
        tabBg.layer.shadowColor = RGBA(0, 0, 0, 0.1).CGColor;
        tabBg.layer.shadowOffset = CGSizeMake(0,0);
        tabBg.layer.shadowOpacity = 1;
        tabBg.layer.shadowRadius = 13;
        [self.view addSubview:_tabbarView];
    }
    
    return _tabbarView;
    
}

- (SDTabbarItem *)homeBtn
{
    if (!_homeBtn) {
        
        _homeBtn = [SDMethodTool getViewWithViewName:NSStringFromClass([SDTabbarItem class])];
         _homeBtn.titleLab.text = @"首页";
        _homeBtn.tabbarImage.selected = YES;
        [_homeBtn.titleLab setTextColor:SDMainColor];
        [_homeBtn.tabbarImage setImage:[UIImage imageNamed:@"首页 拷贝"] forState:UIControlStateNormal];
       
        @weakify(self);
        [_homeBtn.tabbarImage setImage:[UIImage imageNamed:@"首页"] forState:UIControlStateSelected];
        [_homeBtn.selectBtn handleControlEvent:UIControlEventTouchUpInside withBlock:^{
            @strongify(self);
            if (self.selectBtn != self.homeBtn) {
                
                [self.selectBtn.titleLab setTextColor:SDMainTextColor];
                self.selectBtn.tabbarImage.selected = NO;
                self.selectBtn = self.homeBtn;
                self.selectBtn.tabbarImage.selected = YES;
                [self.selectBtn.titleLab setTextColor:SDMainColor];
                self.selectedIndex = 0;
                
            }
            
        }];
        [self.tabbarView addSubview:_homeBtn];
        
    }
    
    return _homeBtn;
    
}
- (SDTabbarItem *)mineBtn
{
    if (!_mineBtn) {
        
        _mineBtn = [SDMethodTool getViewWithViewName:NSStringFromClass([SDTabbarItem class])];
        _mineBtn.titleLab.text = @"我的";
        [_mineBtn.tabbarImage setImage:[UIImage imageNamed:@"我的"] forState:UIControlStateNormal];
        [_mineBtn.tabbarImage setImage:[UIImage imageNamed:@"我的 拷贝"] forState:UIControlStateSelected];
        @weakify(self);
           
        [_mineBtn.selectBtn handleControlEvent:UIControlEventTouchUpInside withBlock:^{
             @strongify(self);
            if (self.selectBtn != self.mineBtn) {
                [self.selectBtn.titleLab setTextColor:SDMainTextColor];
                self.selectBtn.tabbarImage.selected = NO;
                self.selectBtn = self.mineBtn;
                self.mineBtn.tabbarImage.selected = YES;
                [self.selectBtn.titleLab setTextColor:SDMainColor];
                self.selectedIndex = 1;
            }
        }];
        [self.tabbarView addSubview:_mineBtn];
        
    }
    
    return _mineBtn;
    
}
- (SDTabbarItem *)detectionBtn
{
    if (!_detectionBtn) {
        
        _detectionBtn = [SDMethodTool getViewWithViewName:NSStringFromClass([SDTabbarItem class])];
        _detectionBtn.titleLab.text = @"测试";
        
        [_detectionBtn.selectBtn handleControlEvent:UIControlEventTouchUpInside withBlock:^{
           // @strongify(self);
           // [self addQrView];
           
        }];
        [self.tabbarView addSubview:_detectionBtn];
        
    }
    
    return _detectionBtn;
    
}
- (SDDetectionView *)detectionView
{
    if (!_detectionView) {
        
        _detectionView = [SDMethodTool getViewWithViewName:NSStringFromClass([SDDetectionView class])];
        
        _detectionView.layer.masksToBounds = YES;
        _detectionView.layer.cornerRadius = 28.f;
        _detectionView.clipsToBounds = YES;
        [_detectionView.goBtn handleControlEvent:UIControlEventTouchUpInside withBlock:^{
            // @strongify(self);
            //[self addQrView];
//            [self.kindView showView];
            [self goAnswerDetectionVC];
            
        }];
        [self.tabbarView addSubview:_detectionView];
        
    }
    
    return _detectionView;
    
}
- (SDHomeShowView *)kindView
{
    if (!_kindView) {
        _kindView = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([SDHomeShowView class]) owner:nil options:nil].lastObject;
        [_kindView setViews];
        [self.view addSubview:_kindView];
    }
    return _kindView;
}
/**
 二维码页面
 */
- (void)addQrView
{
    self.qrView = (SDQrCodeViewController *)[SDMethodTool getViewControlWithIdentifier:NSStringFromClass([SDQrCodeViewController class])];

    [self.qrView.view setFrame:self.view.bounds];
    [self.view addSubview:self.qrView.view];
    [self addChildViewController:self.qrView];
    
}
- (void)goAnswerDetectionVC
{
    SDAnswerTestAllResultVC *vc = [SDMethodTool getViewControlWithIdentifier:NSStringFromClass([SDAnswerTestAllResultVC class])];
    [self.navigationController pushViewController:vc animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
